var app = getApp();

Page({
  data: {
    flag: true,
    quan: false,
    trip_state: ''
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      tripId: options.tripId
    })
  },
  onShow: function () {
    var that = this;
    that.getData();
  },
  getData: function () {
    var that = this;
    app.JsonPost("Api/Trip/detail", {
      id: that.data.tripId
    }, function (res) {
      console.log(res);
      for (var i = 0; i < res.airway.length; i++) {
        res.airway[i]["show"] = app.timeContrast(res.airway[i]["depart_date"]);
        //res.airway[i]["diff_date"] = app.timeDiff(res.airway[i]["depart_date"], res.airway[i]["reach_date"]);
        res.airway[i]["depart_date"] = app.formatTime(res.airway[i]["depart_date"], 'Y-M-D h:m');
        res.airway[i]["reach_date"] = app.formatTime(res.airway[i]["reach_date"], 'Y-M-D h:m');
      }
      that.setData({
        my: res.user.id == wx.getStorageSync("userId") ? 'my' : 'other',
        promulgator: res.user.user_name,
        promulgator_head: res.user.avatar,
        promulgator_id: res.user.id,
        ticket_id: res.ticket_id,
        add_time: app.formatTime(res.add_time, 'Y-M-D h:m'),
        reward_min: res.reward_min,
        description: res.description,
        isFavorited: res.isFavorited,
        state_text: res.state_text.label,
        surplus_money: res.surplus_money,
        contact: res.contact,
        airway_list: res.airway,
        trip_state: res.state,
        trip_action: res.action,
        status: res.status,
        is_contacted: res.is_contacted,
        money_gain_contact: res.money_gain_contact
      });
    })
  },
  collectAdd: function () {
    var that = this;
    app.JsonPost("Api/TripFavorite/add", {
      trip_id: that.data.tripId
    }, function (res) {
      //console.log(res);
      that.setData({
        isFavorited: true
      });
    })
  },
  collectDel: function (e) {
    var that = this;
    app.JsonPost("Api/TripFavorite/delete", {
      trip_id: that.data.tripId
    }, function (res) {
      //console.log(res);
      that.setData({
        isFavorited: false
      });
    })
  },
  changeStatus: function (e) {
    var status = e.currentTarget.dataset.type;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '点击“确定”将改变该行程的状态',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          var jsonData = {
            id: that.data.tripId,
            state: status
          };
          app.JsonPost("Api/Trip/changeStatus", jsonData, function (res) {
            //console.log(res);
            that.onShow();
          })
        }
      }
    });
  },
  deleteTrip: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '点击“确定”将删除该行程',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          app.JsonPost("Api/Trip/delete", {
            id: that.data.tripId
          }, function (res) {
            //console.log(res);
            wx.navigateBack({
              delta: 1
            });
          })
        }
      }
    });
  },
  showPop: function () {
    var that = this;
    if (parseFloat(that.data.money_gain_contact) > 0) {
      this.setData({
        flag: false
      })
    } else {
      that.setData({
        is_contacted: true,
        flag: true
      });
      wx.createSelectorQuery().select('#j_page').boundingClientRect(function (rect) {
        // 使页面滚动到底部
        wx.pageScrollTo({
          scrollTop: rect.bottom
        })
      }).exec()
    }
  },
  hidePop: function () {
    this.setData({
      flag: true
    })
  },
  payTap: function () {
    var that = this;

    //判断是否实名认证
    if (wx.getStorageSync("certification") == 'false') {
      wx.showModal({
        title: '提示',
        content: '实名认证用户才能发布!',
        showCancel: false,
        success: function (res) {
          wx.navigateTo({
            url: '../my/pages/certification/certification',
          })
        }
      });
      return false;
    }

    wx.showModal({
      title: '提示',
      content: '点击“确定”支付获取联系方式',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          var jsonPayData = {
            trip_id: that.data.tripId,
            type: 2,
            reward: that.data.money_gain_contact,
            deposit: 0,
            brokerage: 0
          };
          app.showLoading("正在提交...请稍后...");
          app.JsonPost("Api/Pay/index", jsonPayData, function (data) {
            app.closeLoading();
            if (data !== false) {
              wx.requestPayment({
                timeStamp: data.timeStamp + "",
                nonceStr: data.nonceStr,
                package: data.package,
                signType: data.signType,
                paySign: data.paySign,
                success(res) {
                  that.setData({
                    is_contacted: true,
                    flag: true
                  });
                  wx.createSelectorQuery().select('#j_page').boundingClientRect(function (rect) {
                    // 使页面滚动到底部
                    wx.pageScrollTo({
                      scrollTop: rect.bottom
                    })
                  }).exec()
                },
                fail(res) {
                  wx.showModal({
                    title: '提示',
                    content: '获取联系方式失败!',
                    showCancel: false,
                    success: function (res) { }
                  });
                }
              })
            }
          })
        }
      }
    });
  },
  telBtn: function (e) {
    var tel = e.currentTarget.dataset.tel;
    wx.showActionSheet({
      itemList: ['复制 ' + tel, '拨打 ' + tel],
      success(res) {
        if (res.tapIndex == 0){
          wx.setClipboardData({
            data: tel,
            success: function (res) {
              wx.getClipboardData({
                success: function (res) {
                  wx.showToast({
                    title: '复制成功'
                  })
                }
              })
            }
          })
        }
        if (res.tapIndex == 1) {
          wx.makePhoneCall({
            phoneNumber: tel,
            success: function () {
              console.log("成功拨打电话")
            }
          })
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  orderTap: function () {
    var that = this;
    wx.navigateTo({
      url: '../order/orderNew?tripId=' + that.data.tripId + '&surplus_money=' + that.data.surplus_money + '&reward_min=' + that.data.reward_min
    })
  },
  quanSelect: function (e) {
    var that = this;
    that.data.quan = !that.data.quan;
    that.setData({
      quan: that.data.quan
    })
  },
  userCredit: function () {
    var that = this;
    wx.navigateTo({
      url: '../user/credit?userId=' + that.data.promulgator_id
    })
  }
})